package Sudoku3.io;
import Sudoku3.structure.cell;
import Sudoku3.structure.grid;


public class textEngine {
	
	public static final String newline = System.getProperty("line.separator");
	
	private grid _grid;
	
	public textEngine(grid g) { _grid = g; }
	
	public void fromString(String s) throws Exception {
		if (!(s.length() == 81 || s.length() == 243)) {throw new Exception("Input String must be 81 or 243 chars. (" + s.length() + ")");}
		

		
		// setting cell value by 81-char value string
		if (s.length() == 81) {
			int index = 0;
			for (int x = 0; x < 9; x++) {
				for (int y = 0; y < 9; y++) { 
					try {
						int v = Integer.parseInt(String.valueOf(s.charAt(index)));
						if (0 < v & v < 10) { _grid.getCell(x, y).setValue(v); }
					} 
					catch(Exception e) { } //do nothing, leave the cell unknown (works for non-numeric chars)
										
					index++;
				}
			}
		}

		else { // setting cells _possible by 243-char state string
			
			int index = 0;
			for (int x = 0; x < 9; x++) {
				for (int y = 0; y < 9; y++) { 
					try {
						int v = Integer.parseInt(String.valueOf(s.substring(index, index+3)));
						_grid.getCell(x, y).fromInt(v);
					} 
					catch(Exception e) { } //do nothing, leave the cell unknown (works for non-numeric chars)
										
					index = index + 3;
				}
			}
		}
	}
	
	public String toStringState() {
		String t = "";

		for (int x = 0; x < 9; x++) {
			for (int y = 0; y < 9; y++) { 
				cell c = _grid.getCell(x, y);				
				t += String.format("%03d", c.toInt()); 
			}
		}
		return t;
	}
	
	//return a string representing the whole board (human-friendly compact)
	public String toStringCompact() {
		String t = "";
		for (int x = 0; x < 9; x++) {
			for (int y = 0; y < 9; y++) { 
				t += _grid.getCell(x, y).getValue(); 
			}
			t += newline;
		}
		t = t.replaceAll("0", ".");
		return t;
	}
	
	public String toStringFull() {
		String DoubleLine = " ######################################################################### ";
		String SingleLine = " #-------+-------+-------#-------+-------+-------#-------+-------+-------# ";
		String t = DoubleLine + newline;
		
		//loop the rows
		for (int r = 0; r < 9; r++) {
			
			//loop through each column 3 times for the 3-lines of each cell (123,456,789)
			for (int x = 0; x < 3; x++) {
				//loop the column for the top row of each cell
				for (int c = 0; c < 9; c++) {
					switch (c) {
						case 0: case 3: case 6: t += " # "; break;
						default: t += " | "; break;
					}
					
					//cell e = _grid.rows[r].get(c);
					cell e = _grid.getCell(r, c);
					
					if (e.getValue() == 0) {
						String t2 = e.toString().substring(3*x, 3*x+3);
						String t3 =  t2.charAt(0) + " " + t2.charAt(1) + " " + t2.charAt(2);
						t += t3;
					}
					else { t+= (x == 1) ? "  " + e.getValue() + "  " : "     "; }
					
				}
				t += " # " + newline;
			}

			switch (r) {
				case 2: case 5: case 8: t += DoubleLine + newline; break;
				default: t += SingleLine + newline; break;
			}
		}
		
		return t;
	}

}
